home *** CD-ROM | disk | FTP | other *** search
- /*
- * WORDS.C - Counts the occurance of characters, words and lines in a given text file.
- *
- * PROGRAMMER: Martti Ylikoski
- * CREATED: 2.12.1990
- */
- static char *VERSION = "Version 1.1. Copyright (c) Martti Ylikoski. 1990, 1991. " ;
- /*
- */
-
- #include <stdio.h>
- #include <malloc.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #include <param.h>
- #include <paramstd.h>
- #define INCL_DOS
- #include <os2.h>
-
-
- static char *progname ;
- extern unsigned long pflags ;
-
- ParamEntry pentry[12] = {
- "P", &ParamSetPause, 0,
- "F", &ParamSetFold, 0,
- "V", &ParamSetVerbose, 0,
- "R", &ParamSetReport, 0,
- "S", &ParamSetSubDirs, 0,
- "?", &ParamSetHelp, 1,
- "H", &ParamSetHelp, 1,
- "NOD", &ParamSetNoDefault, 0,
- "TEST", &ParamSetTest, 0,
- "Y", &ParamSetYes, 0,
- "N", &ParamSetTest, 0,
- "\0", NULL, 0
- } ;
-
- ParamBlock params = {
- "/-", IGNORECASE | NOPRIORITY | NOTRIGGERSALLOWED ,
- pentry
- } ;
-
- /* local prototypes */
- static int words( char *fname) ;
-
- int main(int argc, char *argv[])
- {
- FILE *fptr ;
- int i, c ;
-
- progname = argv[0] ;
-
- ParamHandle(¶ms, &argc, argv) ;
-
- if (pflags & PA_HELP)
- {
- printf("%s - count the occurance of characters, words and lines in a given text file.", progname) ;
- puts(VERSION) ;
- puts("Usage: WORDS file(s) [/H | /?]") ;
- return( 1 ) ;
- }
-
- if (argc == 1)
- words("-") ;
- else
- for ( i = 1 ; i < argc ; i++)
- words(argv[i]) ;
-
- return( 0 ) ;
- }
-
- static int words( char *fname)
- {
- long characters, words, lines ;
- int in_word, in_line ;
- FILE *fptr ;
- int i, c ;
-
- characters = 0L ; words = 0L ; lines = 0L ;
- in_word=FALSE ;
- in_line = FALSE ;
- printf("[%s]\n",fname) ;
-
- if (strcmp(fname, "-") == 0)
- fptr = stdin ;
- else
- /* open file */
- if ((fptr = fopen(fname, "r")) == NULL)
- {
- printf("%s: unable to open file %s ...\n", progname, fname) ;
- printf("Bypassing file...\n") ;
- return( 1 ) ;
- }
-
- /* read from file */
- while ( (c = fgetc(fptr)) != EOF)
- {
- characters ++ ;
- in_line = TRUE ;
- if (isspace(c) != 0 && in_word == TRUE)
- words ++ ;
- else
- if (isspace(c) == 0) /* not a space character */
- in_word = TRUE ;
-
- if (c == '\n')
- {
- in_word = FALSE ;
- in_line = FALSE ;
- lines ++ ;
- }
- }
-
- /* close file */
- fclose (fptr ) ;
-
- if (in_line == TRUE)
- lines ++ ; /* last line not yet counted */
- if (in_word == TRUE)
- words ++ ; /* last word not yet counted */
- printf("characters = %d\nwords = %d\nlines = %d\n\n", characters, words, lines) ;
- return( 0 ) ;
-
- }
-